package com.test.smartcampusapi.controller;

import com.test.smartcampusapi.entity.Bill;
import com.test.smartcampusapi.service.BillService;
import com.test.smartcampusapi.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Date : 4/23/2022 12:09 AM
 * Author : lingy
 * Description : ...
 */
@RestController
public class BillController {
    @Autowired
    private BillService billService;
    @Autowired
    private CardService cardService;

    @PostMapping(value = "/bill/create")
    public int createBill(@RequestBody Map<String, Object> bill) {
        int billType = Integer.parseInt(bill.get("billType").toString());
        double billAmount = Double.parseDouble(bill.get("billAmount").toString());
        String billTime = bill.get("billTime").toString();
        String billBelongCard = bill.get("billBelongCard").toString();
        billService.createBill(billType, billAmount, billTime, billBelongCard);
        // 更新余额
        cardService.updateCredit(billBelongCard, billType * billAmount);//amount始终为正
        return 0;
    }

    @PostMapping(value = "/bill/getByNumber")
    public List<Bill> getByNumber(@RequestBody Map<String, String> bill) {
        try {
            String cardNumber = bill.get("cardNumber");
            return billService.getBillsByCardNumber(cardNumber);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
